﻿using Microsoft.Office.Interop.Visio;
using System;

namespace VisioERLeveller.Visio {
    public class Point {

        public static double EPS { get; set; } = 1e-5;

        public double X { get; set; }

        public double Y { get; set; }

        public Point(double x, double y) {
            this.X = x;
            this.Y = y;
        }

        public static bool operator==(Point a, Point b) {
            var ds2 = (a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y);
            var ds = Math.Sqrt(ds2);
            return ds < EPS;
        }

        public static bool operator!=(Point a, Point b) => !(a==b);

        public static Point operator+(Point a, Point b) {
            return new Point(a.X + b.X, a.Y + b.Y);
        }

        public static Point operator-(Point a) {
            return new Point(-a.X, -a.Y);
        }

        public static Point operator-(Point a, Point b) {
            return new Point(a.X - b.X, a.Y - b.Y);
        }

        public double Module {
            get {
                var ds2 = this.X * this.X + this.Y * this.Y;
                var ds = Math.Sqrt(ds2);
                return ds;
            }
        }

        public Point Direction {
            get {
                var s = this.Module;
                if (s < EPS)
                    throw new ArithmeticException("Vector divided by zero length module");
                return new Point(this.X / s, this.Y / s);
            }
        }

        public override bool Equals(object obj) {
            if (obj is null) return false;
            if (obj is not Point p) return false;
            return this == p;
        }

        public override int GetHashCode() {
            return this.X.GetHashCode() ^ this.Y.GetHashCode();
        }
    }
}
